<?
/**
 * This file is part of XNAT light.
 *
 * XNAT light is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XNAT light is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XNAT light.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace NRG\Framework\Controllers;

use NRG\Framework\Common\Registry as Registry;
use NRG\Framework\Exceptions\IllegalArgumentException as IllegalArgumentException;
use NRG\Database\Mappers as Mappers;

/**
 * Project controller
 *
 * @package NRG\Framework\Controllers
 */
class Project extends BaseController
{
    /**
     * XNAT object
     * 
     * @var Xnat 
     */
    protected $_xnat;

    /**
     * Pre-dispatch of action
     */
    public function preDispatch()
    {
        $this->_xnat = Registry::get("xnat");
    }

    /**
     * Get Project name given a Project ID or a wildcard
     *
     * $_REQUEST["id"]
     * $_REQUEST["like"]
     */
    public function labelAction()
    {
        if($this->_session->hasRequestParam("like"))
        {
            $wildcard = $this->_session->getRequestParam("like", false);

            if(trim($wildcard) == "")
            {
                $this->_view->error = true;
                $this->_view->message = "wildcard cannot be empty";
                return;
            }

            // thwart illegal characters
            if(!$this->validateWildcard($wildcard))
                return;
            
            $wildcard = str_replace("*", "%", $wildcard);
            $wildcard = str_replace("_", "\_", $wildcard);

            $mapper = new Mappers\Project();
            $labels = $mapper->getWildcardLabels($wildcard);

            if($labels)
                $this->_view->vals = $labels;
            else
                $this->_view->vals = array();
        }
        else if($this->_session->hasRequestParam("id"))
        {
            $id = $this->_session->getRequestParam("id", false);

            if(trim($id) == "")
            {
                $this->_view->error = true;
                $this->_view->message = "id cannot be empty";
                return;
            }

            $mapper = new Mappers\Project();
            $project = $mapper->byId($id);

            if($project)
                $this->_view->vals = array($project->getName());
            else
                $this->_view->vals = array();
        }
        else
        {
            $mapper = new Mappers\Project();
            $labels = $mapper->getWildcardLabels("%");

            if($labels)
                $this->_view->vals = array($labels);
            else
                $this->_view->vals = array();
        }
    }

    /**
     * Get Project IDs given a Project name or wildcard
     *
     * $_REQUEST["label"]
     * $_REQUEST["like"]
     */
    public function idAction()
    {
        if($this->_session->hasRequestParam("like"))
        {
            $wildcard = $this->_session->getRequestParam("like", false);

            if(trim($wildcard) == "")
            {
                $this->_view->error = true;
                $this->_view->message = "wildcard cannot be empty";
                return;
            }

            // thwart illegal characters
            if(!$this->validateWildcard($wildcard))
                return;

            $wildcard = str_replace("*", "%", $wildcard);
            $wildcard = str_replace("_", "\_", $wildcard);

            $mapper = new Mappers\Project();
            $ids = $mapper->getWildcardIDs($wildcard);

            if($ids)
                $this->_view->vals = $ids;
            else
                $this->_view->vals = array();
        }
        else if($this->_session->hasRequestParam("label"))
        {
            $label = $this->_session->getRequestParam("label", false);

            if(trim($label) == "")
            {
                $this->_view->error = true;
                $this->_view->message = "label cannot be empty";
                return;
            }

            $mapper = new Mappers\Project();
            $ids = $mapper->getIDs($label);

            if($ids)
                $this->_view->vals = $ids;
            else
                $this->_view->vals = array();
        }
        else
        {
            $mapper = new Mappers\Project();
            $ids = $mapper->getWildcardIDs("%");

            if($ids)
                $this->_view->vals = array($ids);
            else
                $this->_view->vals = array();
        }
    }

    /**
     * Check wildcard for illegal characters
     *
     * @param string $wildcard
     * @return boolean
     */
    private function validateWildcard($wildcard)
    {
        if(!is_string($wildcard))
            throw new IllegalArgumentException("Wildcard must be an instance of string");
        else if(trim($wildcard) == "")
            throw new IllegalArgumentException("Wildcard cannot be empty");
        
        if(preg_match("/^[a-zA-Z0-9_\*]+$/", $wildcard))
            return true;
        else
        {
            $this->_view->error = true;
            $this->_view->message = "wildcard contains illegal characters";

            return false;
        }
    }
}
